package com.haiyi.software.app;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

public class ActivityMyFriend extends FragmentActivity {

	private ImageView imgCursor;// 动画图片
	private ViewPager viewPager;
	private TextView textViewFriends, textViewFans, textViewFollow;
	private int offset = 0;// 动画图片偏移量
	private int currIndex = 0;// 当前页卡编号

	private int imageW;// 图片宽度

	private ArrayList<Fragment> fragmentsList;
	FragmentVPFriends fragmentVPFriends;
	FragmentVPFans fragmentVPFans;
	FragmentVPFollow fragmentVPFollow;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO 自动生成的方法存根
		super.onCreate(arg0);

		setContentView(R.layout.fragment_friend);
		fragmentVPFriends = new FragmentVPFriends();
		fragmentVPFans = new FragmentVPFans();
		fragmentVPFollow = new FragmentVPFollow();

		InitComp();
	}

	// @Override
	// public View onCreateView(LayoutInflater inflater, ViewGroup container,
	// Bundle savedInstanceState) {
	//
	// fragmentVPFriends = new FragmentVPFriends();
	// fragmentVPFans = new FragmentVPFans();
	// fragmentVPFollow = new FragmentVPFollow();
	// this.layoutInflater = inflater;
	// this.this = inflater.inflate(R.layout.fragment_friend,
	// null);
	// InitComp();
	// // TODO 自动生成的方法存根
	// // return super.onCreateView(inflater, container, savedInstanceState);
	// return this;
	// }
	// 初始化组件
	private void InitComp() {

		// TODO 自动生成的方法存根

		InitSliderImage();
		InitTextView();
		InitViewPager();

	}

	/*
	 * 初始化viewpage
	 */
	private void InitViewPager() {

		// TODO 自动生成的方法存根
		viewPager = (ViewPager) this.findViewById(R.id.viewPager_friend);
		fragmentsList = new ArrayList<Fragment>();
		fragmentsList.add(fragmentVPFriends);
		fragmentsList.add(fragmentVPFans);
		fragmentsList.add(fragmentVPFollow);
		viewPager.setAdapter(new MyFragmentPagerAdapter(
				getSupportFragmentManager(), fragmentsList));
		viewPager.setCurrentItem(0);
		viewPager.setOnPageChangeListener(new MyOnPageChangeListener());
	}

	/*
	 * 初始化头
	 */
	private void InitTextView() {

		// TODO 自动生成的方法存根
		textViewFriends = (TextView) this.findViewById(R.id.textView_friends);
		textViewFans = (TextView) this.findViewById(R.id.textView_fans);
		textViewFollow = (TextView) this.findViewById(R.id.textView_follow);

		textViewFriends.setOnClickListener(new MyOnClickListener(0));
		textViewFans.setOnClickListener(new MyOnClickListener(1));
		textViewFollow.setOnClickListener(new MyOnClickListener(2));

	}

	/*
	 * 初始化slider图片
	 */
	private void InitSliderImage() {

		// TODO 自动生成的方法存根
		imgCursor = (ImageView) this.findViewById(R.id.img_slider);
		WindowManager wManager = (WindowManager) this
				.getSystemService(this.WINDOW_SERVICE);
		int screenW = wManager.getDefaultDisplay().getWidth();// 获取分辨率宽度
		imageW = (int) (screenW / 3);
		offset = 0;// 计算偏移量
		ViewGroup.LayoutParams params = imgCursor.getLayoutParams();
		params.width = imageW;
		imgCursor.setLayoutParams(params);
		// Log.i("宽度",String.valueOf(screenW));
	}

	/**
	 * 页卡切换监听
	 */
	public class MyOnPageChangeListener implements OnPageChangeListener {

		int one = offset * 2 + imageW;// 页卡1 -> 页卡2 偏移量

		int two = one * 2;// 页卡1 -> 页卡3 偏移量

		@Override
		public void onPageSelected(int arg0) {

			Animation animation = null;
			switch (arg0) {
			case 0:
				if (currIndex == 1) {
					animation = new TranslateAnimation(one, 0, 0, 0);
				} else if (currIndex == 2) {
					animation = new TranslateAnimation(two, 0, 0, 0);
				}
				break;
			case 1:
				if (currIndex == 0) {
					animation = new TranslateAnimation(offset, one, 0, 0);
				} else if (currIndex == 2) {
					animation = new TranslateAnimation(two, one, 0, 0);
				}
				break;
			case 2:
				if (currIndex == 0) {
					animation = new TranslateAnimation(offset, two, 0, 0);
				} else if (currIndex == 1) {
					animation = new TranslateAnimation(one, two, 0, 0);
				}
				break;
			}
			currIndex = arg0;
			animation.setFillAfter(true);// True:图片停在动画结束位置
			animation.setDuration(400);
			imgCursor.startAnimation(animation);
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {

		}

		@Override
		public void onPageScrollStateChanged(int arg0) {

		}
	}

	/**
	 * 头标点击监听
	 */
	private class MyOnClickListener implements View.OnClickListener {

		private int index = 0;

		public MyOnClickListener(int i) {
			this.index = i;
		}

		@Override
		public void onClick(View v) {
			viewPager.setCurrentItem(this.index);

		}
	};
}
